package com.ss.android.article.base.feature.detail.presenter;

import android.app.Activity;
import android.support.v4.util.SparseArrayCompat;

import com.bytedance.common.utility.Logger;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public final class ActivityStackManager {
    public enum Type {
        DETAIL_ACTIVITY(4),
        WENDA_ACTIVITY(5),
        CONCERN_ACTIVITY(3);
        int maxActivityNum;

        Type(int i) {
            this.maxActivityNum = i;
        }

        public final int getMaxActivityNum() {
            return this.maxActivityNum;
        }
    }


    private static SparseArrayCompat<List<WeakReference<Activity>>> a = new SparseArrayCompat();

    private static void checkAndFinishActivity(Type type) {
        if (type != null) {
            List<WeakReference<Activity>> activityRefList = ActivityStackManager.a.get(type.ordinal());
            int maxActivityNum = type.getMaxActivityNum();
            Logger.d("ActivityStackManager", "checkAndFinishActivity, activityRefList.size() = " + activityRefList.size());
            if (activityRefList != null && activityRefList.size() > maxActivityNum) {
                int v2 = activityRefList.size() - maxActivityNum;
                Iterator<WeakReference<Activity>> iterator = activityRefList.iterator();
                for (maxActivityNum = 0; iterator.hasNext(); ++maxActivityNum) {
                    if (maxActivityNum >= v2) {
                        return;
                    }
                    Activity activity = iterator.next().get();
                    if (activity != null) {
                        activity.finish();
                    }
                    iterator.remove();
                }
            }
        }
    }

    public static void addActivity(Type type, Activity activity) {
        if (type != null && activity != null) {
            Logger.d("ActivityStackManager", "addActivity, activity.hashCode() = " + activity.hashCode());
            int v1 = type.ordinal();
            Object v0 = ActivityStackManager.a.get(v1);
            if (v0 == null) {
                ArrayList v0_1 = new ArrayList();
                ActivityStackManager.a.put(v1, v0_1);
                v0 = v0_1;
            }

            if (!ActivityStackManager.containsActivity(((List) v0), activity)) {
                ((List) v0).add(new WeakReference(activity));
            }

            ActivityStackManager.checkAndFinishActivity(type);
            Logger.d("ActivityStackManager", "afetr checkAndFinishActivity, activity List for type " + type + " is as follows: ");
            ActivityStackManager.b(type);
        }
    }

    private static boolean containsActivity(List arg4, Activity arg5) {
        if (arg4 == null || arg5 == null) {
            return false;
        } else {
            Logger.d("ActivityStackManager", "containsActivity, activity.hashCode() = " + arg5.hashCode());
            Iterator v2 = arg4.iterator();
            do {
                if (v2.hasNext()) {
                    if (arg5 != ((WeakReference) v2.next()).get()) {
                        continue;
                    }
                    break;
                } else {
                    return false;
                }
            } while (true);
            Logger.d("ActivityStackManager", "containsActivity, activityRefList contains Activity " + arg5);
            return true;
        }
    }

    private static void b(Type arg6) {
        if ((Logger.debug()) && arg6 != null) {
            Object v0 = ActivityStackManager.a.get(arg6.ordinal());
            if (v0 != null) {
                Iterator v2 = ((List) v0).iterator();
                int v0_1;
                int v1 = 0;
                for (v0_1 = 0; v2.hasNext(); v0_1 = v1) {
                    v1 = v0_1 + 1;
                    v0 = ((WeakReference) v2.next()).get();
                    if (v0 != null) {
                        Logger.d("ActivityStackManager", "i = " + v1 + ", activity.hashCode() = " + v0.hashCode());
                    }
                }
            }
        }
    }

    public static void removeActivity(Type type, Activity activity) {
        if (type != null && activity != null) {
            int v2 = type.ordinal();
            Object v0 = ActivityStackManager.a.get(v2);
            if (v0 != null && ((List) v0).size() != 0) {
                Logger.d("ActivityStackManager", "removeActivity, activity.hashCode() = " + activity.hashCode());
                Iterator iterator = ((List) v0).iterator();
                do {
                    if (iterator.hasNext()) {
                        if (activity != ((WeakReference) iterator.next()).get()) {
                            continue;
                        }
                        break;
                    }

                    if (((List) v0).size() == 0) {
                        ActivityStackManager.a.remove(v2);
                    }
                    ActivityStackManager.b(type);
                    return;
                } while (true);
                Logger.d("ActivityStackManager", "removeActivity, activityRefList contains Activity " + activity);
                iterator.remove();
                if (((List) v0).size() == 0) {
                    ActivityStackManager.a.remove(v2);
                }
                ActivityStackManager.b(type);
            }
        }
    }
}

